package com.seezoon.infrastructure.error;

import com.seezoon.infrastructure.exception.ErrorDefinition;

public enum ErrorCode implements ErrorDefinition {
    /**
     * 建议公共错误定义在1000-2000 的范围
     */
    UNKNOWN(1000, "系统错误，请稍后重试"),

    PARAM_INVALID(1001, "param invalid"),

    SQL_ERROR(1002, "数据库操作错误", true),
    ASSERTION_ERROR(1003, "assertion error"),
    RECORD_NOT_EXISTS(1004, "记录不存在"),

    TCP_START_MAGIC_NOT_MATCH(2001, "read tcp message start magic not match"),
    TCP_CHECKSUM_NOT_MATCH(2002, "read tcp message checksum not match"),
    TCP_MESSAGE_FORMAT_ERROR(2003, "read tcp message format error"),
    TCP_MESSAGE_UNKNOWN(2004, "read tcp message unknown msg id"),
    TCP_MESSAGE_SERIALIZATION_ERROR(2005, "read tcp message serialization error"),
    TCP_MESSAGE_TOO_LARGE(2006, "read tcp message too large"),
    TCP_MESSAGE_READ_TIMEOUT(2007, "read tcp message timeout"),
    TCP_CHANNEL_CLOSED(2008, "tcp channel closed"),

    DEVICE_NOT_ONLINE(3000, "device not online"),
    DEVICE_OFFLINE(3001, "device is offline"),
    DEVICE_SEND_MSG_ERROR(3002, "device send msg error"),
    ;


    public static final int ERROR_TYPE_BUSINESS = 0;
    public static final int ERROR_TYPE_SYSTEM = 1;

    private int code;
    private String msg;

    private int type;


    ErrorCode(int code, String msg) {
        this(code, msg, false);
    }

    ErrorCode(int code, String msg, boolean sysError) {
        this.code = code;
        this.msg = msg;
        this.type = sysError ? ERROR_TYPE_SYSTEM : ERROR_TYPE_BUSINESS;
    }

    @Override
    public int code() {
        return code;
    }

    @Override
    public String msg() {
        return msg;
    }

    @Override
    public int type() {
        return type;
    }

    public boolean IsSystemError() {
        return type == ERROR_TYPE_SYSTEM;
    }
}
